<?php

namespace app\admin\controller;

use app\common\validate\Upload;
use think\Controller;
use think\Request;
use app\common\model\Test;
use app\common\controller\Http;
use app\common\lib\Util;
use app\common\model\UserProfile as user_profile;

class Order extends Base
{
    /**
     * @return mixed 上传图片
     */
    public function uploadImage(Request $request)
    {
        return $this->fetch('upload');
    }

    public function doUploadImage(Request $request,Upload $validate,Http $http)
    {
        //上传目录
        $uploadPath = './upload/';
        if(!is_dir($uploadPath)){
            mkdir($uploadPath,0777);
        }
        $file = $request->file('img');
        $data = array(
            'auth'=>$request->auth,
            //字段名必须是__token__
            '__token__'=>$request->token,
            'image'=> $file
        );
        $res = $validate->check($data);
        if(!$res){
            return Util::apiResponse([],$validate->getError(),-1);
        }
        //移动文件
        $info = $file->move($uploadPath);
        if($info){
            // 获取上传信息
            $url = $uploadPath.$info->getSaveName();
            //to do
            return Util::apiResponse([],$url,1);
        }else{
            return Util::apiResponse([],'上传失败',-1);
        }
    }

    /**
     * @return mixed 富文本
     */
    public function editor(){
        $image = Test::where(['id'=>5])->field('image')->find();
        $image = 'http://'.$_SERVER['SERVER_NAME'].'/'.$image['image'];
        $this->assign('image',$image);
        return $this->fetch();
    }

    public function doEditor(Request $request,Upload $validate,Test $test,Http $http)
    {
        //上传目录
        $uploadPath = './upload/';
        if(!is_dir($uploadPath)){
            mkdir($uploadPath,0777);
        }
        $file = $request->file('img');
        if($file){
            $data = ['image'=> $file];
            $validateResult =  $validate->check($data);
            if(!$validateResult){
                return apiResponse([],$file->getError(),-1);
            }
            //移动文件
            $info = $file->move($uploadPath);
            if($info){
                // 获取上传信息
                $url = $uploadPath.$info->getSaveName();
                //to do
            }else{
                return Util::apiResponse([],'上传失败',-1);
            }
        }else{
            $url = '';
        }
        $detail = $request->detail;
        $res = $test->save(['image'=>$url,'detail'=>$detail]);
        if(!$res){
            return Util::apiResponse([],'操作失败',-1);
        }
        return Util::apiResponse([],$detail,1);
    }

    public function index(user_profile $user_profile){
       $userOrder = $user_profile->order()->select();
       $this->assign('userOrder',$userOrder);
       return $this->fetch();
    }

    public function edit(){

    }
}
